package com.sui.bigdata.sml.web.repository.mapper;

import com.sui.bigdata.sml.web.dto.ModelDTO;
import com.sui.bigdata.sml.web.repository.model.FieldInfo;
import com.sui.bigdata.sml.web.repository.model.Model;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author songhaicheng
 * @date 2019/12/17 10:45
 * @description
 * @reviewer
 */
@Repository
public interface ModelMapper {

    @Insert("insert into sml_models(token,hdfs_path,model_types,version,creator,source,description,online,field_infos,user_id)" +
            " values(#{token},#{hdfsPath},#{modelTypes},#{version},#{creator},#{source},#{description}," +
            "#{online},#{fieldInfos},#{userId})")
    @Options(useGeneratedKeys = true)
    int insert(Model model);

    @Select("select * from sml_models where id=#{id}")
    Model get(Long id);

    @Select("<script>" +
            "select * from sml_models where token=#{token}" +
            "<if test='version!=null'>" +
            " and `version`=#{version}" +
            "</if>" +
            "<if test='online!=null'>" +
            " and `online`=#{online}" +
            "</if>" +
            " order by version desc" +
            "</script>")
    List<Model> listByTokenWithVersionAndOnline(@Param("token") String token, @Param("version") Long version, @Param("online") Boolean online);

    /**
     * 只允许更改模型描述信息和上下线信息
     *
     * @param models
     * @return
     */
    @Update("<script>" +
            "<foreach collection='models' item='model' open='' separator=';' close=''>" +
            "update sml_models set creator=#{model.creator},description=#{model.description},field_infos=#{model.fieldInfos}" +
            "<if test='model.online!=null'>" +
            ",online=#{model.online}" +
            "</if>" +
            " where id=#{model.id}" +
            "</foreach>" +
            "</script>")
    int updateBatch(@Param("models") List<Model> models);

    @Select("<script>" +
            "select * from (select * from sml_models where online=1" +
            "<if test='userId!=null'> and user_id=#{userId}</if>" +
            " union " +
            "select * from sml_models a where online=0" +
            "<if test='userId!=null'> and user_id=#{userId}</if>" +
            " and token not in (" +
            "select token from sml_models where online=1" +
            "<if test='userId!=null'> and user_id=#{userId}</if>" +
            ") and version=(" +
            "select max(version) from sml_models b where b.token=a.token))" +
            " c order by online desc, update_time desc" +
            "</script>")
    List<Model> listByUserId(@Param("userId") Long userId);

    @Delete("delete from sml_models where id=#{id}")
    int delete(Long id);

    /**
     * 待删除
     *
     * @param token
     * @param version
     * @param fieldInfos
     * @return
     */
    @Insert({
            "<script>",
            "insert into model_field_nafill(token, version, field,data_type,value) values ",
            "<foreach collection='fieldInfos' item='fieldInfo' index='index' separator=','>",
            "(#{token}, #{version}, #{fieldInfo.field},#{fieldInfo.dataType},#{fieldInfo.value})",
            "</foreach>",
            "</script>"
    })
    @Options(useGeneratedKeys = true)
    int insertFieldInfo(@Param("token") String token, @Param("version") int version, @Param("fieldInfos") List<FieldInfo> fieldInfos);

}
